package com.paw.ruoyi.admin.common.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.paw.ruoyi.admin.common.utils.ExcelHeaderAliasUtil.HeaderAliasDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 基于hutool excel 导入类
 * 对于大量数据使用 BigExcelWriter
 * @author rubble
 * @date 2022/1/24
 **/
public class HuExcelUtil {

  private static Logger logger = LoggerFactory.getLogger(HuExcelUtil.class);

  /**
   * ExcelWriter 文档 + 自定义别名 ExcelHeaderAliasUtil
   *
   * @param filePath
   * @param rows
   * @param clazz
   * @param <T>
   */
  public static <T> void write (String filePath, List<T> rows, Class<T> clazz) {
    // 通过工具类创建writer
    ExcelWriter writer = ExcelUtil.getWriter(filePath);

    //自定义标题别名
    List<HeaderAliasDto> headerAlias = ExcelHeaderAliasUtil.getHeaderAlias(clazz);
    headerAlias.sort(Comparator.comparing(HeaderAliasDto::getSort));
    for (HeaderAliasDto item : headerAlias) {
      writer.addHeaderAlias(item.getField(), item.getAlias());
    }

    // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
    writer.setOnlyAlias(true);

    // 一次性写出内容，使用默认样式，强制输出标题
    writer.write(rows, true);
    // 关闭writer，释放内存
    writer.close();
  }

  public static <T>  void write (HttpServletResponse response, List<T> rows, Class<T> clazz) {

    ExcelWriter writer = ExcelUtil.getWriter(true);

    //自定义标题别名
    List<HeaderAliasDto> headerAlias = ExcelHeaderAliasUtil.getHeaderAlias(clazz);
    headerAlias.sort(Comparator.comparing(HeaderAliasDto::getSort));
    for (HeaderAliasDto item : headerAlias) {
      writer.addHeaderAlias(item.getField(), item.getAlias());
    }

    // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
    writer.setOnlyAlias(true);

    writer.write(rows, true);

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");

    ServletOutputStream out = null;
    try {
      out = response.getOutputStream();
      writer.flush(out, true);
    } catch (IOException e) {
      logger.error("writeResponse error", e);
    }
    writer.close();
    IoUtil.close(out);
  }

  public static <T> void write (OutputStream out, List<T> rows, Class<T> clazz) {

    ExcelWriter writer = ExcelUtil.getWriter(true);

    //自定义标题别名
    List<HeaderAliasDto> headerAlias = ExcelHeaderAliasUtil.getHeaderAlias(clazz);
    headerAlias.sort(Comparator.comparing(HeaderAliasDto::getSort));
    for (HeaderAliasDto item : headerAlias) {
      writer.addHeaderAlias(item.getField(), item.getAlias());
    }

    // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
    writer.setOnlyAlias(true);

    writer.write(rows, true);
    writer.flush(out, true);
    writer.close();
    IoUtil.close(out);
  }


}
